package com.my.service.impl;


import com.my.service.GoodsService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private LoadBalancerClient loadBalancerClient;//ribbon负载均衡器

    private String getUrl(String str) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("goods-provider");
        return "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/goods/" + str;
    }

    @Override
    public String addGoods(String name, Double price, String imgUrl, Integer stock, String info, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", name);
        map.add("price", price);
        map.add("imgUrl", imgUrl);
        map.add("stock", stock);
        map.add("info", info);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("add"), request, String.class);
        return response.getBody();
    }

    @Override
    public String deleteGoods(Integer goodsId) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
        map.add("goodsId", goodsId);
        HttpEntity<MultiValueMap<String, Integer>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("delete"), request, String.class);
        return response.getBody();
    }

    @Override
    public String deleteGoods(String goodsIds) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("goodsIds", goodsIds);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("deleteGoods"), request, String.class);
        return response.getBody();
    }

    @Override
    public String reverseGoods(Integer goodsId) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
        map.add("goodsId", goodsId);
        HttpEntity<MultiValueMap<String, Integer>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("reverse"), request, String.class);
        return response.getBody();
    }

    @Override
    public String updateGoods(Integer id, String name, Double price, String imgUrl, Integer stock, String info, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        map.add("name", name);
        map.add("price", price);
        map.add("imgUrl", imgUrl);
        map.add("stock", stock);
        map.add("info", info);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("update"), request, String.class);
        return response.getBody();
    }

    @Override
    public String showGoods(String context, Integer currentPage, Integer pageSize, String sort, Boolean asc, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("context", context);
        map.add("currentPage", currentPage);
        map.add("pageSize", pageSize);
        map.add("sort", sort);
        map.add("asc", asc);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        System.out.println(sort);
        System.out.println(request);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("show"), request, String.class);
        return response.getBody();
    }

    @Override
    public String showGoodsForUser(String context, Integer currentPage, Integer pageSize, String sort, Boolean asc) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("context", context);
        map.add("currentPage", currentPage);
        map.add("pageSize", pageSize);
        map.add("sort", sort);
        map.add("asc", asc);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        System.out.println(request);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("showForUser"), request, String.class);
        return response.getBody();
    }

    @Override
    public String selById(Integer id) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        HttpEntity<MultiValueMap<String, Integer>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("selById"), request, String.class);
        return response.getBody();
    }

    @Override
    public String buyGoods(Integer num, Integer goodsId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
        map.add("num", num);
        map.add("goodsId", goodsId);
        HttpEntity<MultiValueMap<String, Integer>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("buy"), request, String.class);
        return response.getBody();
    }
}
